﻿@page "/identity/users"
@attribute [Authorize(Policy = Permissions.Users.View)]
@inject Microsoft.Extensions.Localization.IStringLocalizer<Users> _localizer
@using BlazorHero.CleanArchitecture.Application.Responses.Identity

<style>
    .mud-table-container {
        overflow: auto;
    }
</style>

<HeroTitle Title="@_localizer["Users"]" Description="@_localizer["Manage Users"]" />
@if (!_loaded)
{
    <MudProgressCircular Color="Color.Primary" Indeterminate="true" />
}
else
{
    <MudTable Hover="true" Elevation="25" Items="_userList" Dense="@_dense" Bordered="@_bordered" Striped="@_striped" Filter="new Func<UserResponse, bool>(Search)" @bind-user="_user">
        <ToolBarContent>
            <div class="justify-center mud-text-align-center">
                @if (_canCreateUsers)
                {
                    <MudButton DisableElevation Variant="Variant.Filled" Color="Color.Primary" OnClick="(() => InvokeModal())" StartIcon="@Icons.Material.Filled.Add" IconColor="Color.Surface">@_localizer["Register New User"]</MudButton>
                    <MudButton DisableElevation Variant="Variant.Filled" OnClick="GetUsersAsync" StartIcon="@Icons.Material.Filled.Refresh" IconColor="Color.Surface" Color="Color.Secondary">@_localizer["Reload"]</MudButton>
                    @if (_canExportUsers)
                    {
                        <MudButton DisableElevation Variant="Variant.Filled" OnClick="ExportToExcel" StartIcon="@Icons.Custom.FileFormats.FileExcel" IconColor="Color.Secondary" Color="Color.Surface" Style="margin-left: 5px;">@_localizer["Export"]</MudButton>
                    }
                }
                else
                {
                    <MudButton DisableElevation Variant="Variant.Filled" OnClick="GetUsersAsync" StartIcon="@Icons.Material.Filled.Refresh" IconColor="Color.Surface" Color="Color.Secondary">@_localizer["Reload"]</MudButton>
                    @if (_canExportUsers)
                    {
                        <MudButton DisableElevation Variant="Variant.Filled" OnClick="ExportToExcel" StartIcon="@Icons.Custom.FileFormats.FileExcel" IconColor="Color.Secondary" Color="Color.Surface" Style="margin-left: 5px;">@_localizer["Export"]</MudButton>
                    }
                }
            </div>
            <MudSpacer />
            @if (_canSearchUsers)
            {
                <MudTextField @bind-Value="_searchString" Immediate="true" FullWidth=false Placeholder="@_localizer["Search For Users"]" Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0 mb-3"></MudTextField>
            }
        </ToolBarContent>
        <HeaderContent>
            <MudTh><MudTableSortLabel SortBy="new Func<UserResponse, object>(x => x.FirstName)">@_localizer["First Name"]</MudTableSortLabel></MudTh>
            <MudTh><MudTableSortLabel SortBy="new Func<UserResponse, object>(x => x.LastName)">@_localizer["Last Name"]</MudTableSortLabel></MudTh>
            <MudTh><MudTableSortLabel SortBy="new Func<UserResponse, object>(x => x.UserName)">@_localizer["UserName"]</MudTableSortLabel></MudTh>
            <MudTh><MudTableSortLabel SortBy="new Func<UserResponse, object>(x => x.Email)">@_localizer["Email"]</MudTableSortLabel></MudTh>
            <MudTh><MudTableSortLabel SortBy="new Func<UserResponse, object>(x => x.PhoneNumber)">@_localizer["PhoneNumber"]</MudTableSortLabel></MudTh>
            <MudTh><MudTableSortLabel SortBy="new Func<UserResponse, object>(x => x.EmailConfirmed)">@_localizer["Email Confirmation"]</MudTableSortLabel></MudTh>
            <MudTh><MudTableSortLabel SortBy="new Func<UserResponse, object>(x => x.IsActive)">@_localizer["Active"]</MudTableSortLabel></MudTh>
            <MudTh Style="text-align:right">@_localizer["Actions"]</MudTh>
        </HeaderContent>
        <RowTemplate>
            <MudTd DataLabel="FirstName">
                <MudHighlighter Text="@context.FirstName" HighlightedText="@_searchString" />
            </MudTd>
            <MudTd DataLabel="LastName">
                <MudHighlighter Text="@context.LastName" HighlightedText="@_searchString" />
            </MudTd>
            <MudTd DataLabel="UserName">
                <MudHighlighter Text="@context.UserName" HighlightedText="@_searchString" />
            </MudTd>
            <MudTd DataLabel="Email">
                <MudHighlighter Text="@context.Email" HighlightedText="@_searchString" />
            </MudTd>
            <MudTd DataLabel="PhoneNumber">
                <MudHighlighter Text="@context.PhoneNumber" HighlightedText="@_searchString" />
            </MudTd>
            <MudTd DataLabel="EmailConfirmed"><MudCheckBox @bind-Checked="@context.EmailConfirmed" ReadOnly Color="Color.Secondary"></MudCheckBox></MudTd>
            <MudTd DataLabel="Active"><MudCheckBox @bind-Checked="@context.IsActive" ReadOnly Color="Color.Secondary"></MudCheckBox></MudTd>
            <MudTd DataLabel="Actions" Style="text-align:right">
                <MudMenu Label="@_localizer["Actions"]" Variant="Variant.Filled" DisableElevation="true" EndIcon="@Icons.Filled.KeyboardArrowDown" IconColor="Color.Secondary" Direction="Direction.Left" OffsetX="true">
                    <MudMenuItem OnClick="(()=>ViewProfile(context.Id))">@_localizer["View Profile"]</MudMenuItem>
                    @if (_canViewRoles)
                        {
                        <MudMenuItem OnClick="(()=>ManageRoles(context.Id, context.Email))">@_localizer["Manage Roles"]</MudMenuItem>
                        }
                </MudMenu>
            </MudTd>
        </RowTemplate>
        <FooterContent>
            <MudSwitch @bind-Checked="@_dense" Color="Color.Secondary" Style="margin-left: 5px;">@_localizer["Dense"]</MudSwitch>
            <MudSwitch @bind-Checked="@_striped" Color="Color.Tertiary" Style="margin-left: 5px;">@_localizer["Striped"]</MudSwitch>
            <MudSwitch @bind-Checked="@_bordered" Color="Color.Warning" Style="margin-left: 5px;">@_localizer["Bordered"]</MudSwitch>
        </FooterContent>
        <PagerContent>
            <TablePager />
        </PagerContent>
    </MudTable>
}